package com.gitee.feizns.v8.rander;

import java.util.Random;

/**
 * @author feizns
 * @since 2019/8/13
 */
public class Utils {

    /**
     * 位数
     * @return
     */
    public String randomNumber() {
        return randomNumber(10);
    }

    public static String randomNumber(int n) {
        Random random = new Random();
        StringBuilder ret = new StringBuilder();
        ret.append((int) (Math.ceil(Math.random() * 10)));
        for (int i = 1; i < n; i++) {
            ret.append(random.nextInt(10));
        }
        return ret.toString();
    }

    public String concatDbAndUrl(String db, String url) {
        int index = url.indexOf("?");
        if ( index == -1 )
            return url;
        return new StringBuilder(url).insert(index, db).toString();
    }

    public String existsCode(Object obj) {
        StringBuilder rt = new StringBuilder("4" + obj.toString());
        while ( rt.length() < 4 ) {
            rt.insert(1, "0");
        }
        return rt.toString();
    }

    public String notExistsCode(Object obj) {
        StringBuilder rt = new StringBuilder("44" + obj.toString());
        while ( rt.length() < 4 ) {
            rt.insert(2, "0");
        }
        return rt.toString();
    }

}
